package org.edu.modules.oa.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 配置web的跨域问题
 * @author leidq
 * @create 2020-08-18 15:08
 */
@Configuration
public class CorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        org.springframework.web.cors.CorsConfiguration corsConfiguration=new org.springframework.web.cors.CorsConfiguration();
        //配置跨域
        corsConfiguration.addAllowedHeader("*"); //配置请求头
        corsConfiguration.addAllowedMethod("*"); //配置请求方法
        corsConfiguration.addAllowedOrigin("*"); //配置来源
        corsConfiguration.setAllowCredentials(true);//是否允许携带cookie跨域（允许）

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
